﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Libs.Db;

namespace Libs.HR
{
    public class Employees
    {
        public int EmployeeID { get; set; }
        public string ManualID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Avatar { get; set; }
        public DateTime Birthday { get; set; }
        public int Gender { get; set; }
        public string Email { get; set; }
        public int DepartmentID { get; set; }
        public int OfficeID { get; set; }
        public DateTime JoinedTime { get; set; }
        public int ContractTypeID { get; set; }
        public string WorkPhone { get; set; }
        public string Mobile { get; set; }
        public DateTime LogTime { get; set; }
        public int ReturnValue { get; set; }

        public Employees()
        {

        }

        ~Employees()
        {

        }

        public virtual void Dispose()
        {

        }

        /// <summary>
        /// Lấy giá trị Employees theo ID
        /// </summary>
        /// <param name="employeeID"></param>
        /// <returns></returns>
        public Employees Get(int employeeID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<Employees>("sp_Employees_Select",
                new SqlParameter("@EmployeeID", employeeID));
        }

        /// <summary>
        /// Lấy giá trị Employees theo ID
        /// </summary>
        /// <returns></returns>
        public Employees Get()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<Employees>("sp_Employees_Select",
                new SqlParameter("@EmployeeID", EmployeeID));
        }

        /// <summary>
        /// Thêm mới Employees
        /// </summary>
        public void Add()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[14];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@ManualID", ManualID);
            pars[2] = new SqlParameter("@FirstName", FirstName);
            pars[3] = new SqlParameter("@LastName", LastName);
            pars[4] = new SqlParameter("@Avatar", Avatar);
            pars[5] = new SqlParameter("@Birthday", Birthday);
            pars[6] = new SqlParameter("@Gender", Gender);
            pars[7] = new SqlParameter("@Email", Email);
            pars[8] = new SqlParameter("@DepartmentID", DepartmentID);
            pars[9] = new SqlParameter("@OfficeID", OfficeID);
            pars[10] = new SqlParameter("@JoinedTime", JoinedTime);
            pars[11] = new SqlParameter("@ContractTypeID", ContractTypeID);
            pars[12] = new SqlParameter("@WorkPhone", WorkPhone);
            pars[13] = new SqlParameter("@Mobile", Mobile);

            db.ExecuteNonQuerySP("sp_Employees_Insert", pars);
            EmployeeID = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Cập nhật Contract
        /// </summary>
        public void Update()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[15];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@EmployeeID", EmployeeID);
            pars[2] = new SqlParameter("@ManualID", ManualID);
            pars[3] = new SqlParameter("@FirstName", FirstName);
            pars[4] = new SqlParameter("@LastName", LastName);
            pars[5] = new SqlParameter("@Avatar", Avatar);
            pars[6] = new SqlParameter("@Birthday", Birthday);
            pars[7] = new SqlParameter("@Gender", Gender);
            pars[8] = new SqlParameter("@Email", Email);
            pars[9] = new SqlParameter("@DepartmentID", DepartmentID);
            pars[10] = new SqlParameter("@OfficeID", OfficeID);
            pars[11] = new SqlParameter("@JoinedTime", JoinedTime);
            pars[12] = new SqlParameter("@ContractTypeID", ContractTypeID);
            pars[13] = new SqlParameter("@WorkPhone", WorkPhone);
            pars[14] = new SqlParameter("@Mobile", Mobile);

            db.ExecuteNonQuerySP("sp_Employees_Update", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Xoá Contract
        /// </summary>
        /// <param name="contractID"></param>
        public void Delete(int contractID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_Employees_Delete",
                new SqlParameter("@ContractID", contractID));
        }

        /// <summary>
        /// Xoá Contract
        /// </summary>
        public void Delete()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_Employees_Delete",
                new SqlParameter("@EmployeeID", EmployeeID));
        }

        //public DataTable GetTList()
        //{
        //    DbHelper db = new DbHelper(Config.HRConnectionStrings);
        //    return db.GetDataTableSP("sp_Employees_SelectList");
        //}

        public List<Employees> List()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetListSP<Employees>("sp_Employees_SelectList");
        }

        public DataTable GetTList(int departmentID, int contractTypeID, int pageIndex, int pageSize, ref int totalRecord)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[5];
            pars[0] = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = departmentID == 0 ? new SqlParameter("@DepartmentID", DBNull.Value) : new SqlParameter("@DepartmentID", departmentID);
            pars[2] = contractTypeID == 0 ? new SqlParameter("@ContractTypeID", DBNull.Value) : new SqlParameter("@ContractTypeID", contractTypeID);
            pars[3] = new SqlParameter("@PageIndex", pageIndex);
            pars[4] = new SqlParameter("@PageSize", pageSize);

            DataTable dt = db.GetDataTableSP("sp_Employees_SelectListPage", pars);
            totalRecord = Convert.ToInt32(pars[0].Value);
            return dt;
        }

        public DataTable GetTListBirthday(int departmentID, DateTime dateMin, DateTime dateMax, int pageIndex, int pageSize, ref int totalRecord)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[6];
            pars[0] = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = departmentID == 0 ? new SqlParameter("@DepartmentID", DBNull.Value) : new SqlParameter("@DepartmentID", departmentID);
            pars[2] = new SqlParameter("@DateMin", dateMin);
            pars[3] = new SqlParameter("@DateMax", dateMax);
            pars[4] = new SqlParameter("@PageIndex", pageIndex);
            pars[5] = new SqlParameter("@PageSize", pageSize);

            DataTable dt = db.GetDataTableSP("sp_Employees_SelectBirthdayPage", pars);
            totalRecord = Convert.ToInt32(pars[0].Value);
            return dt;
        }

        public DataTable TListJoinedTime(int departmentID, int pageIndex, int pageSize, ref int totalRecord)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[4];
            pars[0] = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = departmentID == 0 ? new SqlParameter("@DepartmentID", DBNull.Value) : new SqlParameter("@DepartmentID", departmentID);
            pars[2] = new SqlParameter("@PageIndex", pageIndex);
            pars[3] = new SqlParameter("@PageSize", pageSize);

            DataTable dt = db.GetDataTableSP("sp_Employees_SelectJoinedTimeListPage", pars);
            totalRecord = Convert.ToInt32(pars[0].Value);
            return dt;
        }

    }
}
